<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Miya\Models\Area;
use App\Miya\Models\Banner;
use App\Miya\Models\ChatRecord;
use App\Miya\Models\Debris;
use App\Miya\Models\DerivativeKeyword;
use App\Miya\Models\Info;
use App\Miya\Models\Keyword;
use App\Miya\Models\Module;
use App\Miya\Models\Msg;
use App\Miya\Models\Section;
use Encore\Admin\Auth\Database\Menu;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->title('系统总览')
            //版本更新提醒
            //->body($this->versionUpdateRemind())
            //短信数量充值提醒
            ->body($this->noteBalanceRemind())
            //关键词更新提示
            ->body($this->keywordUpdateRemind())
            //快捷菜单
            ->body(Admin::user()->isAdministrator() ? $this->showMenus() : '')
            //关键词相关
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $column->append($this->showModuleInfos());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->showSearchWords());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->showAreaKeywords());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->showKeywordInfos());
                });
            })->row(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->append($this->showUserViews());
                });
                $row->column(6, function (Column $column) {
                    $column->append($this->showIpSort());
                });
            });
    }

    protected function versionUpdateRemind()
    {
        if (request()->getClientIp() == '127.0.0.1') {
            $month = now()->format('Y.m');
            $version = config('version.version');

            if ($version < $month) {
                return view('miya::home._version_update_remind');
            }
        }
    }

    protected function showIpSort()
    {
        return view('miya::home._ip_sort');
    }

    /**
     * 每日访问量统计
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showUserViews()
    {
        return view('miya::home._user_views');
    }

    /**
     * 模块下信息统计
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showModuleInfos()
    {
        return view('miya::home._module_infos');
    }

    /**
     * 展示关键词对应的信息数量
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showKeywordInfos()
    {
        return view('miya::home._keyword_infos');
    }

    /**
     * 展示关键词相关信息
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showDerivativeKeywords()
    {
        return view('miya::home._derivative_keywords');
    }

    protected function showSearchWords()
    {
        return view('miya::home._search_words');
    }

    /**
     * 展示关键词相关信息
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showAreaKeywords()
    {
        return view('miya::home._area_keywords');
    }

    /**
     * 展示快捷菜单
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function showMenus()
    {
        //允许显示的快捷菜单id
        $needMenuId = [9, 12, 29, 30, 31, 15, 28, 33, 8, 11, 36];
        $menuCount = [
            9 => 0,
            12 => 0,
            29 => 0,
            30 => 0,
            31 => 0,
            15 => 0,
            28 => 0,
            33 => Msg::where('is_read', 2)->count(),
            8 => 0,
            11 => 0,
            36 => ChatRecord::where('status', 1)->count(),
        ];
        $menus = Menu::where('parent_id', 0)
            ->whereIn('id', $needMenuId)->orderBy('order')->get()->map(function ($menu) use ($menuCount) {
                $menu->recordNum = $menuCount[$menu->id];
                return $menu;
            });

        return view('miya::home._nav', ['menus' => $menus]);
    }

    //短信数量提示
    protected function noteBalanceRemind()
    {
        //未开通不提醒
        if (!config('setting.phone_hint')) return;
        //未填写手机号不提醒
        if (config('setting.phone') < 11) return;
        //数量充盈不提醒
        if (config('note.balance') >= 10) return;
        return view('miya::home._not_balance_remind');
    }

    // 关键词更新提示
    protected function keywordUpdateRemind()
    {
        $lastRecord = Keyword::whereNotNull('updated_at')->orderBy('updated_at', 'DESC')->first();
        //不存在关键词的情况
        if (!$lastRecord) return;

        if ($lastRecord->updated_at->addDay() < now()) return;

        return view(
            'miya::home._keyword_update_remind',
            ['time' => $lastRecord->updated_at->addDay()->diffForHumans(now())]
        );
    }
}
